package com.spring.aop.jdkproxy;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.springframework.cglib.proxy.InvocationHandler;

public class JdkProxy implements java.lang.reflect.InvocationHandler  {

	
	private UserDao userDao;
	
	public JdkProxy(UserDao userDao) {
		this.userDao=userDao;
		
	}
	/*
	 * 产生UserDao的代理方法  ------代理对象
	 */
	
	
       public UserDao createProxy() {
    	   
    	UserDao userDaoProxy = (UserDao) Proxy.newProxyInstance(userDao.getClass().getClassLoader(), userDao.getClass().getInterfaces(), this);
		return userDaoProxy;
    	   
       }

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		//判断方法是否为save
		
		if ("save".equals(method.getName())) {
			
			//增强
			System.out.println("代码增强功能添加，比如权限校验===========");
			return method.invoke(userDao, args);
			
		}
		return method.invoke(userDao, args);
	}
}
